home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / hypercar / xfcn / spttool.cpt / Support Tools eXternals 1.2.5 / card_33586.txt < prev    next >
Text File  |  1990-11-13  |  9KB  |  245 lines

  1. -- card: 33586 from stack: in.5
  2. -- bmap block id: 9152
  3. -- flags: 0000
  4. -- background id: 3858
  5. -- name: FileExists
  6. ----- HyperTalk script -----
  7. on HideObjects
  8.   hide cd btn "Try It!"
  9. end HideObjects
  10.  
  11. on ShowObjects
  12.   show cd btn "Try It!"
  13. end ShowObjects
  14.  
  15.  
  16. -- part 2 (button)
  17. -- low flags: 00
  18. -- high flags: A002
  19. -- rect: left=82 top=185 right=219 bottom=175
  20. -- title width / last selected line: 0
  21. -- icon id / first selected line: 0 / 0
  22. -- text alignment: 1
  23. -- font id: 0
  24. -- text size: 12
  25. -- style flags: 8192
  26. -- line height: 16
  27. -- part name: Try it!
  28. ----- HyperTalk script -----
  29. on mouseUp
  30.   global errGlobal
  31.   ask "Enter some full path name"
  32.   if it = empty then exit mouseUp
  33.   put fileExists(it,"nodialog:errGlobal") into itIsThere
  34.   if last char of it = ":" then put "folder" into type
  35.   else put "file" into type
  36.   if itIsThere = "true" then
  37.     answer "Good guess!  That" && type && "exists!"
  38.   else
  39.     answer "Sorry, that" && type && "does not exist!"
  40.   end if
  41. end mouseUp
  42.  
  43.  
  44.  
  45.  
  46. -- part contents for background part 38
  47. ----- text -----
  48. 16/50
  49.  
  50. -- part contents for background part 20
  51. ----- text -----
  52. FileExists - An XFCN to test whether or not a file/folder exists on a mounted volume.
  53.  
  54. FileExists(pathname, ┬½"noDialog:"errorGlobal┬╗)
  55.  
  56. This XFCN returns TRUE or FALSE depending on whether or not the file/folder specified by  Pathname is on a mounted volume.  It is useful to check for things like preferences stacks which your stack needs to install.
  57.  
  58.  
  59.  
  60. -- part contents for background part 42
  61. ----- text -----
  62. { FileExits(pathname ┬½,"nodialog":errGlobal┬╗)                 }
  63. {}
  64. { XFCN to determin whether or not the specified file/folder   }
  65. { exists.  Makes no modifications to the file/folder.           }
  66. { Returns TRUE if the file/folder exists and FALSE if it does     }
  67. { not.  Assumes a full pathName is give in the parameter       }
  68. { PathName.                                                        }
  69. {}
  70. {   Written by:      Anup Murarka         Eric Carlson         }
  71. {               ALINK:  SKEPTIC       ALINK:  cyNic   }
  72. {                                   CIS:  76004,3356         }
  73. {}
  74. {               We are part of the Support Tools Development Group,     }
  75. {               Apple Computer, Inc.      }
  76. {}
  77. {               please DO NOT contack Mac DTS for support of this code!    }
  78. {}
  79. {               please DO contact the authors for support of this code!     }
  80. {}
  81. {               Send comments, bug reports, requests to any of the above   }
  82. {               E-mail addresses or to:}
  83. {}
  84. {                           (one of us)                  }
  85. {                           Apple Computer, Inc.          }
  86. {                           900 E. Hamilton, Ave.          }
  87. {                           Campbell, CA   95008      }
  88. {                           M/S 72-L                     }
  89. {}
  90. {   Copyright:   ┬⌐ 1989, 1990 by Apple Computer, Inc., all rights reserved.     }
  91. {}
  92. { written by    : Anup Murarka                                                                               }
  93. { AppleLink  : Skeptic                                                                                      }
  94. { modification history                                                                                        }
  95. {          Date              Initials                                    Comments                               }
  96. {          ----          ------          ------------------------------------------------------}
  97. {       8/16/89           akm         first written                                                           }
  98. {       5/22/90           ec            removed upper case converion for A/UX compatibility.       }
  99. {                                           Changed version to 1.1                                          }
  100. {}
  101. unit FileExists;
  102.  
  103. interface
  104.  
  105.     uses
  106.         HyperXCmd;
  107.  
  108.     procedure MAIN (paramPtr: XCmdPtr);
  109.  
  110. implementation
  111.  
  112.     procedure FileExists (paramPtr: XCmdPtr);
  113.     FORWARD;
  114.  
  115.     procedure MAIN (paramPtr: XCmdPtr);
  116.     begin
  117.         FileExists(paramPtr);
  118.     end;
  119.  
  120.     procedure reportToUser (paramPtr: XCmdPtr;
  121.                                     msgStr: str255);
  122. {}
  123. { report something back to the user.  }
  124. { the last parameter (optional) to an external may contain }
  125.  { "noDialog" or "noDialog:GlobalName".  GlobalName is the name }
  126.  { of a HyperTalk global variable into which error messages will be }
  127.  { placed.  we've decided to use this approach to avoid confusing }
  128. { an error message with a valid result being returned from an XFCN. }
  129. {}
  130.         var
  131.             tempStr: str255;
  132.     begin
  133. {check the last param to see if the user requested that}
  134. { we suppress the error dialog }
  135.         ZeroToPas(paramPtr, paramPtr^.params[paramPtr^.paramCount]^, tempStr);
  136.         UprString(tempStr, true);
  137.         if pos('NODIALOG', tempStr) = 0 then
  138.     { no special error handling specified, throw up a dialog and return the error message }
  139.             begin
  140.                 SendCardMessage(paramPtr, concat('answer "', msgStr, '"'));
  141.                 paramPtr^.returnValue := PasToZero(paramPtr, msgStr);
  142.             end
  143.         else if (pos(':', tempStr) > 0) then
  144.     { requested global AND noDialog so we fill in the global and return empty }
  145.             begin
  146.                 tempStr := copy(tempStr, pos(':', tempStr) + 1, length(tempStr));
  147.                                                         { get the name of the HC global  to fill }
  148.                 SetGlobal(paramPtr, tempStr, PasToZero(paramPtr, msgStr));
  149.                                                         { and fill it }
  150.                 paramPtr^.returnValue := PasToZero(paramPtr, '');      { return empty }
  151.             end
  152.         else
  153.     { requested noDialog only so we return the error condition as the result }
  154.             paramPtr^.returnValue := PasToZero(paramPtr, msgStr);
  155.     end;     { procedure }
  156.  
  157.     function AskedForHelp (paramPtr: XCmdPtr;
  158.                                     syntaxMsg: Str255;
  159.                                     copyrightMsg: Str255): boolean;
  160. {   check to see if the user sent a '?' or a '!' as }
  161. { the only parameter. if so we will respond with }
  162. { the calling syntax or the copyright/version info }
  163. { for this external }
  164. {}
  165.         var
  166.             firstStr: str255;
  167.     begin
  168.         askedForHelp := false;
  169.         if paramPtr^.paramCount = 1 then
  170.             begin
  171.                 ZeroToPas(paramPtr, paramPtr^.params[1]^, firstStr);
  172.                     { what is the first param? }
  173.                 if firstStr = '?' then
  174.                     begin
  175.                         reportToUser(paramPtr, syntaxMsg);
  176.                         askedForHelp := true
  177.                     end  { asked for help }
  178.                 else if firstStr = '!' then
  179.                     begin
  180.                         reportToUser(paramPtr, copyRightMsg);
  181.                         askedForHelp := true
  182.                     end;     { asked for copyright info }
  183.             end;     { one parameter passed }
  184.     end;     { function }
  185.  
  186.     function getParams (paramPtr: XCmdPtr;
  187.                                     var PathToFind: str255): boolean;
  188.         var
  189.             numParams: integer;
  190.             inputCh, syntaxStr, copyRtStr: str255;
  191.  
  192.     begin
  193.         getParams := true;
  194.         syntaxStr := 'FileExists(pathname ┬½, ΓÇ£noDialogΓÇ¥:errGlobal┬╗)';
  195.         copyRtStr := '┬⌐ 1989,1990 Apple Computer, Inc., v.1.1, by Anup Murarka';
  196.         if askedForHelp(paramPtr, syntaxStr, copyRtStr) then
  197.             begin
  198.                 getParams := false;
  199.                 exit(getParams);
  200.             end;
  201.  
  202.         numParams := paramPtr^.paramCount;
  203.         if (numParams < 1) or (numParams > 2) then
  204.             begin
  205.                 getParams := false;
  206.                 reportToUser(paramPtr, syntaxStr);
  207.                 exit(getParams);
  208.             end;
  209.  
  210.         ZeroToPas(paramPtr, paramPtr^.Params[1]^, PathToFind);
  211.     end;         {GetParams}
  212.  
  213.     procedure FileExists (paramPtr: XCmdPtr);
  214.         var
  215.             getParamsOK: boolean;
  216.             PathToFind: str255;
  217.             paramBlock: CInfoPBRec;
  218.             errorCode: OSErr;
  219.  
  220.     begin
  221.     { fetch and validate the passed parameters}
  222.         getParamsOK := getParams(paramPtr, PathToFind);
  223.         if not (getParamsOK) then
  224.             exit(FileExists);
  225.  
  226.     { initialize parameter block.  Since PathToFind is a full pathname, no other field is needed}
  227.         zeroBytes(paramPtr, @paramBlock, sizeOf(paramBlock));
  228.         paramBlock.ioNamePtr := @PathToFind;
  229.  
  230.     { get file/folder info}
  231.         errorCode := PBGetCatInfo(@paramBlock, FALSE);
  232.  
  233.         if errorCode = noErr then
  234.             paramPtr^.returnValue := pasToZero(paramPtr, 'TRUE')
  235.         else if (errorCode = dirNFErr) or (errorCode = fnfErr) then
  236.             paramPtr^.returnValue := pasToZero(paramPtr, 'FALSE')
  237.         else
  238.             begin
  239.                 paramPtr^.returnValue := pasToZero(paramPtr, 'FALSE');
  240.                 reportToUser(paramPtr, 'Sorry, an unexpected error has occured');
  241.             end;
  242.  
  243.     end;         { procedure FileExists}
  244.  
  245. end.     { unit FileExists}